package cn.school.common;

import cn.school.entity.Student;
import cn.school.service.StudentService;
import cn.school.util.StringUtil;
import cn.school.vo.AjaxPage;
import cn.school.vo.Page;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class StudentController {

        public void checkPhone(HttpServletResponse resp, HttpServletRequest req) throws ServletException, IOException {
            String phone=req.getParameter("phone");
            //phone.substring(11);//故意有异常

            boolean exist=StudentService.INST.checkPhone(phone);
            //返回返回给视图？
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resp.getWriter().print(exist);//!!!!!!!!!!!!!!!!!!!
        }

        public String search(HttpServletResponse resp, HttpServletRequest req) throws ServletException, IOException {
            int pageNo= StringUtil.toInt(req.getParameter("pageNo"),1);
            String sname=req.getParameter("sname");
            String qq=req.getParameter("qq");
            String phone=req.getParameter("phone");
            req.setAttribute("sname",StringUtil.trim(sname));
            req.setAttribute("qq",StringUtil.trim(qq));
            req.setAttribute("phone",StringUtil.trim(phone));
            Page<Student> p = StudentService.INST.search(pageNo, Page.PAGE_SIZE, sname, qq, phone, null, null);
            req.setAttribute("p",p);
            req.getRequestDispatcher("/WEB-INF/jsp/student/student2.jsp").forward(req,resp);
            return null;//return "/WEB-INF/jsp/student/student.jsp"
        }

    public void search2(HttpServletResponse resp, HttpServletRequest req) throws ServletException, IOException {
        int pageNo= StringUtil.toInt(req.getParameter("pageNo"),1);
        String sname=req.getParameter("sname");
        String qq=req.getParameter("qq");
        String phone=req.getParameter("phone");
        req.setAttribute("sname",StringUtil.trim(sname));
        req.setAttribute("qq",StringUtil.trim(qq));
        req.setAttribute("phone",StringUtil.trim(phone));
        Page<Student> p = StudentService.INST.search(pageNo, Page.PAGE_SIZE, sname, qq, phone, null, null);
        String s2= JSON.toJSONStringWithDateFormat(p,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);

        resp.getWriter().print(s2);

    }
    //
    public void search3(HttpServletResponse resp, HttpServletRequest req) throws ServletException, IOException {
        int pageNo= StringUtil.toInt(req.getParameter("page"),1);
        int limit= StringUtil.toInt(req.getParameter("limit"),10);
        String sname=req.getParameter("sname");
        String qq=req.getParameter("qq");
        String phone=req.getParameter("phone");
        req.setAttribute("sname",StringUtil.trim(sname));
        req.setAttribute("qq",StringUtil.trim(qq));
        req.setAttribute("phone",StringUtil.trim(phone));
        AjaxPage p = StudentService.INST.search2(pageNo, limit, sname, qq, phone, null, null);
        String s2= JSON.toJSONStringWithDateFormat(p,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);

        resp.getWriter().print(s2);

    }
}
